package cn.tmpan.project.utils;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author tieMinPan
 * @Desc: excel 导入导出工具
 * @date 2019-07-25 15:26
 */
@Slf4j
public class ExcelUtil {

    /**
     * 导出excel
     *
     * @param fileName 保存文件名
     * @param path     保存路径
     * @param sheet
     * @param list     数据
     * @return
     */
    public static boolean generateExcelByXlsx(String fileName, String path, Sheet sheet, List list) {
        OutputStream out = null;
        try {
            out = new FileOutputStream(path + "/" + fileName + ExcelTypeEnum.XLSX.getValue());
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            writer.write(list, sheet);
            writer.finish();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出Excel错误:{}", e.getMessage());
            return false;
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
